package com.regis.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.regis.comment.CodeMsg;
import com.regis.comment.GlobalException;
import com.regis.entity.db.Category;
import com.regis.entity.db.Dish;
import com.regis.entity.db.Setmeal;
import com.regis.entity.vo.ResultVo;
import com.regis.service.CategoryService;
import com.regis.mapper.CategoryMapper;
import com.regis.service.DishService;
import com.regis.service.SetmealService;
import org.apache.ibatis.annotations.Select;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
* @author 纱雾休比酱
* @description 针对表【category(菜品及套餐分类)】的数据库操作Service实现
* @createDate 2022-07-28 03:58:08
*/
@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category>
    implements CategoryService{

    @Autowired
    private CategoryMapper categoryMapper;
    @Autowired
    private DishService dishService;
    @Autowired
    private SetmealService setmealService;

    /**
     * 根据id删除分类
     * @param id
     * @return
     */
    @Override
    public ResultVo<String> remove(Long id) {
        //判断是否有关联
        long count1 = dishService.count(new LambdaQueryWrapper<Dish>().eq(Dish::getCategoryId, id));
        long count2 = setmealService.count(new LambdaQueryWrapper<Setmeal>().eq(Setmeal::getCategoryId, id));
        System.out.println(count1);
        System.out.println(count2);
        if (count1 > 0){
            throw new GlobalException(new CodeMsg("当前分类下关联了菜品"));
        } else if (count2 > 0){
            throw new GlobalException(new CodeMsg("当前分类下关联了套餐"));
        } else {
            super.removeById(id);
            return ResultVo.success("删除成功");
        }
    }
}




